package com.qiya.LiaoDa;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.qiya.LiaoDa.Util.MySqLiteHelper;

public class LoginActivity extends AppCompatActivity {

        MySqLiteHelper helper;
        private EditText user, password;
        private Button login, register;
        private String name;
        private String psd;

        @Override
        protected void onCreate (Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            //数据库操作
            helper = new MySqLiteHelper(this, "SAVE_USER.db", null, 1);
            SQLiteDatabase database = helper.getReadableDatabase();
            Cursor cursor = database.rawQuery("select * from user", null);
            if (cursor.moveToFirst()) {
                do {
                    //从数据库获取到用户名、密码
                    name = cursor.getString(cursor.getColumnIndex("name"));
                    psd = cursor.getString(cursor.getColumnIndex("password"));
                } while (cursor.moveToNext());
                //关闭游标
                cursor.close();
            }
            initViews();
        }

        private void initViews () {
            user = findViewById(R.id.edit_login);
            password = findViewById(R.id.edit_pwd);
            login = findViewById(R.id.btn_denglu);
            register = findViewById(R.id.btn_zhuche);
            //注册按钮点击事件
            register.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(LoginActivity.this, ResterActivity.class);
                    startActivity(intent);
                }
            });
            //登录按钮点击事件
            //验证当前账号、密码是否跟注册报讯在数据库的账号、密码一致，一致则允许登录
            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //获取输入框文本
                    String username = user.getText().toString().trim();
                    String mpassword = password.getText().toString().trim();
                    //database.query("user",null,null,null,null,null,null);
                    if (username.equals("") && mpassword.equals("")) {
                        Toast.makeText(LoginActivity.this, "学号/密码不能为空", Toast.LENGTH_LONG).show();
                    } else {
                        sureuser(username, mpassword);
                    }
                }
            });
        }
        private void sureuser (String username, String mpassword){
            if (username.equals(name) && mpassword.equals(psd)) {
                //账号密码校验正确
                // 登陆成功，跳转的用户界面
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                //提示
                Toast.makeText(LoginActivity.this, "登录成功！", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(LoginActivity.this, "学号/密码错误！", Toast.LENGTH_LONG).show();
            }
        }
    }